package com.ebt.m.wiki.adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.ebt.m.R;
import com.ebt.m.adapter.AccordionAdapter;
import com.ebt.m.data.entity.ProductFeature;
import com.ebt.m.utils.DataValidation;
import com.ebt.m.widget.Accordion;

import java.util.List;

/**
 * Created by Damon.Han on 2017/9/7 0007.
 *
 * @author Damon
 */

public class FeatureAccordionAdapter extends AccordionAdapter<ProductFeature> {
    private Context mContext;
    private Accordion accordionFeature;
    // 列表展開背景
    private int expandedBackgroundColor = Color.parseColor("#fff7ffee");
    private List<ProductFeature> list;

    public FeatureAccordionAdapter(Context context, Accordion accordionFeature, List<ProductFeature> list) {
        super(context, list);
        this.mContext = context;
        this.accordionFeature = accordionFeature;
        this.list = list;

    }

    @Override
    public View getHeaderView(int position, ViewGroup parent) {
        View headerView = LayoutInflater.from(mContext).inflate(R.layout.wiki_accordation_header, parent, false);
        TextView txtTitle = (TextView) headerView.findViewById(R.id.title);
        ProductFeature productFeature = list.get(position);
        txtTitle.setText(productFeature.getTitle());
        if (productFeature.getTitle() != null) {
            headerView.findViewById(R.id.arrow).setVisibility(DataValidation.isEmpty(productFeature.getTitle().trim()) ? View.GONE : View.VISIBLE);
        }
        return headerView;
    }

    @Override
    public View getContentView(int position, ViewGroup parent) {
        View contentView = LayoutInflater.from(mContext).inflate(R.layout.wiki_accordation_content, parent, false);
        return contentView;
    }

    @Override
    public boolean ifCanExpand(int position) {
        return position >= 0;
    }

    @Override
    public void onExpanded(int position, View headerView) {
        super.onExpanded(position, headerView);
        View contentView = accordionFeature.getContentView(position);
        TextView txtContent = (TextView) contentView.findViewById(R.id.content);
        ImageView img = (ImageView) headerView.findViewById(R.id.arrow);
        txtContent.setText(list.get(position).getContent());
        setExpandableStatus(list.get(position), headerView, contentView, img, true);
    }

    @Override
    public void onCollapsed(int position, View headerView) {
        super.onCollapsed(position, headerView);
        ImageView img = (ImageView) headerView.findViewById(R.id.arrow);
        ProductFeature config = list.get(position);
        setExpandableStatus(config, headerView, accordionFeature.getContentView(position), img, false);
    }

    /**
     * 设置列表展开状态
     *
     * @param header
     * @param content
     * @param arrow
     * @param expanded
     */
    private void setExpandableStatus(ProductFeature config, View header, View content, ImageView arrow, boolean expanded) {
        if (DataValidation.isEmpty(config.getContent().trim())) {
            arrow.setVisibility(View.GONE);
            content.setVisibility(View.GONE);
        } else {
            if (expanded) { // 展开
                if (arrow != null) {
                    arrow.setImageResource(R.drawable.icon_arrow_up_green);
                }
                if (header != null) {
                    header.setBackgroundColor(expandedBackgroundColor);
                }
                if (content != null) {
                    content.setBackgroundColor(expandedBackgroundColor);
                }
            } else { // 收回
                if (arrow != null) {
                    arrow.setImageResource(R.drawable.icon_arrow_down_green);
                }
                if (header != null) {
                    header.setBackgroundColor(Color.WHITE);
                }
                if (content != null) {
                    content.setBackgroundColor(Color.WHITE);
                }
            }
        }
    }
}